package com.wang.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.wang.user.model.User;
import com.wang.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description: 用户
 * @author：tuimao
 * @date: 2024/11/10
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @GetMapping("/test")
    public String test(@RequestParam("name") String name){
        logger.info("log info name:{}", name);
        User test = userService.Test("a1001");
        return JSONUtil.toJsonStr(test);
    }

    // 测试登录，浏览器访问： http://localhost:8066/user/doLogin?username=zhang&password=123456
    @GetMapping("/doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }

    //退出登录
    @PostMapping("/logout")
    public String logout() {
        Object loginId = StpUtil.getLoginId();
        System.out.println(loginId);
        StpUtil.logout();
        return "注销成功";
    }

    // 查询登录状态，浏览器访问： http://localhost:8066/user/isLogin
    @GetMapping("/isLogin")
    public String isLogin() {
        Object loginId = StpUtil.getLoginId();
        System.out.println(loginId);
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
}
